package com.qingcloud.adminbackend.utils;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.AlgorithmParameters;
import java.security.Key;
import java.security.Security;

/**
 * AES加密工具
 */
public class AesUtil {

    private static byte[] passwordBytes;
    private static byte[] ivBytes;

    static {
        try {
            //导入支持AES/CBC/PKCS7Padding的Provider
            Security.addProvider(new BouncyCastleProvider());
            passwordBytes = "198801061A2B3C4D".getBytes("UTF-8");
            ivBytes = "201707051D2C3B4A".getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 蛙鸣AES加密
     *
     * @param content 加密原内容
     * @return
     */
    public static String encrypt(String content) {
        try {
            //指定算法，模式，填充方式，创建一个Cipher
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");

            //生成Key对象
            Key sKeySpec = new SecretKeySpec(passwordBytes, "AES");

            //把向量初始化到算法参数
            AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
            params.init(new IvParameterSpec(ivBytes));

            //指定用途，密钥，参数 初始化Cipher对象
            cipher.init(Cipher.ENCRYPT_MODE, sKeySpec, params);
            //指定加密
            byte[] result = cipher.doFinal(content.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder();
            String temp;
            for (int n = 0; n < result.length; n++) {
                temp = (Integer.toHexString(result[n] & 0xFF));
                sb.append((temp.length() == 1) ? "0" + temp : temp);
            }
            return sb.toString().toUpperCase().trim();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密
     *
     * @param encryptedData 密文
     * @return
     * @throws Exception
     */
    public static String decrypt(String encryptedData) {
        try {
            //指定算法，模式，填充方法 创建一个Cipher实例
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
            //生成Key对象
            Key sKeySpec = new SecretKeySpec(passwordBytes, "AES");
            //把向量初始化到算法参数
            AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
            params.init(new IvParameterSpec(ivBytes));
            //指定用途，密钥，参数 初始化Cipher对象
            cipher.init(Cipher.DECRYPT_MODE, sKeySpec, params);
            int len = encryptedData.length() / 2;
            byte[] ret = new byte[len];
            for (int i = 0; i < len; i++) {
                ret[i] = (byte) Integer.parseInt(encryptedData.substring(i * 2, i * 2 + 2), 16);
            }
            //执行解密
            byte[] result = cipher.doFinal(ret);
            //解密后转成字符串
            return new String(result, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
